/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

/**
 * Created by yfyang on 4/12/14.
 */
define([ 'adminapp','bootstrap-daterangepicker','kindeditor', 'bootstrap-timepicker'], function ($app) {

    KindEditor.create('textarea#plot',{
        themeType:'simple',
        uploadJson: g.ctx + 'file/editor'
    });

    var prvview_tpl = Handlebars.compile($(g.preview_template).html());
    var playtime_tpl = Handlebars.compile($(g.playtime_template).html());

    // nav menu
    $app.navactive('movie_info');

    $app.formvalidator({
        'movie.score'       : {
            number: true
        },
        'movie.price'       : {
            number: true
        }
    }, 'movieInfo');

    $app.uploader({'uploader': g.ctx + 'file/up?dpi=logo', 'buttonText': '点击上传影片封面', width: 140}, 'logoAttachmentUpload');

    $app.uploader({
        'uploader': g.ctx + 'file/up?dpi=env',
        'buttonText'     : '点击上传影片海报', width: 140,
        queueSizeLimit   : 100,
        'onUploadSuccess': function (file, data, response) {
            var jsonData = eval('(' + data + ')');
            if (jsonData['status'] == 'OK') {
                var attachement_path = jsonData['data'];
                var $moPrictures = $('#mo_prictures');
                var val = $moPrictures.val();
                if (val) {
                    $moPrictures.val((val + ",") + attachement_path);
                } else {
                    $moPrictures.val(attachement_path);
                }

                $('#pricture-grid').append(prvview_tpl({ctx: g.ctx, data: attachement_path.replace('.', '.thumbnail.'), preview: attachement_path}))

            }
        }
    }, 'prictureAttachmentUpload');
    $app.formvalidator({}, 'brandForm');

    $('#pricture-grid').on('a.mix-link[data-hook="remove-pic"]', 'click', function (e) {
        e.preventDefault();
        var $this = $(this);
        $this.closest('div.mix').remove();
        return false;
    });

    /*$('input[name="play_time"]').datetimepicker({
        language      : 'zh-CN',
        autoclose     : true,
        isRTL         : App.isRTL(),
        format        : "yyyy-mm-dd",
        pickerPosition: (App.isRTL() ? "bottom-right" : "bottom-left")
    });*/

    timepicker();
    $('#play_time_el').delegate('button[name="oper_play_time"]', 'click', function() {
        var oper = $(this).attr('oper');
        if (oper == 'add') {
            var ctx = {id : new Date().getTime()}
            $('#play_time_el').append(playtime_tpl(ctx));
            /*$('input[name="play_time"]').datetimepicker({
                language      : 'zh-CN',
                autoclose     : true,
                isRTL         : App.isRTL(),
                format        : "yyyy-mm-dd",
                pickerPosition: (App.isRTL() ? "bottom-right" : "bottom-left")
            });*/
            timepicker();
        } else {
            $(this).parent().parent().prev().remove();
            $(this).parent().parent().remove();
        }
        //alert($('#play_time_el').index($(this)));
    });

    function timepicker() {
        $('.timepicker-24').timepicker({
            autoclose: true,
            minuteStep: 1,
            showSeconds: false,
            showMeridian: false
        });
        $('.timepicker').parent('.input-group').on('click', '.input-group-btn', function(e){
            e.preventDefault();
            $(this).parent('.input-group').find('.timepicker').timepicker('showWidget');
        });
    }
});